package com.example.bootdemo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.bootdemo.pojo.ServiceResponse;
import com.example.bootdemo.pojo.UserBlogEntity;
import com.example.bootdemo.pojo.UserBlogKey;
import com.example.bootdemo.service.UserBlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userBlog")
@Slf4j
public class UserBlogController {

    @Autowired
    private UserBlogService userBlogService;



    @PostMapping("/oneUserBlog")
    public ServiceResponse<UserBlogEntity> saveOneUserBlog(@RequestBody UserBlogEntity blogEntity){
        log.info("blogEntity={}", JSON.toJSON(blogEntity));
        ServiceResponse<UserBlogEntity> response = new ServiceResponse<>();
        UserBlogEntity data = userBlogService.saveOne(blogEntity);
        response.setData(data);
        return response;
    }

    @PostMapping("/batchUserBlog")
    public ServiceResponse<List<UserBlogEntity>> batchSaveUserBlog(@RequestBody List<UserBlogEntity> blogEntities){

        log.info("blogEntities={}", JSONArray.toJSONString(blogEntities,true));

        ServiceResponse<List<UserBlogEntity>> response = new ServiceResponse<>();

        List<UserBlogEntity> data = userBlogService.saveAll(blogEntities);

        response.setData(data);

        return response;
    }

    @GetMapping("/findOne")
    public ServiceResponse<UserBlogEntity> findByBlogKey(@RequestParam("createUserId") Integer createUserId,
                                                         @RequestParam("title") String title){
        log.info("createUserId={},title={}",createUserId,title);
        ServiceResponse<UserBlogEntity> response = new ServiceResponse<>();
        UserBlogEntity blogEntity = userBlogService.findById(new UserBlogKey(createUserId,title));
        response.setData(blogEntity);
        return  response;

    }


}
